//creates rect object
//left, top, width, height
sCanvas.fn.rect = function() {
	if (arguments.length !== 1 && arguments.length !== 4) {
		throw new Error("Wrong arguments was passed to rect().");
	}
	
	var attrs,
		obj = new CanvasRect(this);
	
	if (arguments.length === 1) {
		attrs = arguments[0];
	} else {
		attrs = {
			x: arguments[0] + arguments[2] / 2,
			y: arguments[1] + arguments[3] / 2,
			width: arguments[2],
			height: arguments[3]
		};
	}

	obj.attr(attrs);

	this.register(obj);
	this.autorepaint();
	return obj;
};



var CanvasRect = function(sc) {
	this.init(sc);
	this.addSupportedAttr(["width", "height"]);
	this.addAnimatableAttr(["width", "height"]);
	this.initAttrs();
	return this;
};

CanvasRect.prototype = new sCanvasObject();

CanvasRect.prototype.createFillPath = function(cx) {
	var center = this.center();
	
	cx.save();
	cx.translate(this.attrs.x, this.attrs.y);
	cx.rotate(this.attrs.rotation);
	cx.beginPath();
	cx.moveTo(
		-center.x - this.origin.x,
		-center.y - this.origin.y
	);
	cx.lineTo(
		-center.x - this.origin.x + this.attrs.width,
		-center.y - this.origin.y
	);
	cx.lineTo(
		-center.x - this.origin.x + this.attrs.width,
		-center.y - this.origin.y + this.attrs.height
	);
	cx.lineTo(
		-center.x - this.origin.x,
		-center.y - this.origin.y + this.attrs.height
	);
	cx.lineTo(
		-center.x - this.origin.x,
		-center.y - this.origin.y
	);
	cx.closePath();
	cx.restore();
};

CanvasRect.prototype.createStrokePath = function(cx) {
	var center = this.center();
	
	//modificators ensures that rect will not be blurred
	var modX = (this.attrs.lineWidth) % 2 === 1 ? 0.5 : 0;
	var modY = (this.attrs.lineWidth) % 2 === 1 ? 0.5 : 0;
	
	cx.save();
	cx.translate(this.attrs.x, this.attrs.y);
	cx.rotate(this.attrs.rotation);
	cx.beginPath();
	cx.moveTo(
		-center.x - this.origin.x + modX,
		-center.y - this.origin.y + modY
	);
	cx.lineTo(
		-center.x - this.origin.x + this.attrs.width + modX,
		-center.y - this.origin.y + modY
	);
	cx.lineTo(
		-center.x - this.origin.x + this.attrs.width + modX,
		-center.y - this.origin.y + this.attrs.height + modY
	);
	cx.lineTo(
		-center.x - this.origin.x + modX,
		-center.y - this.origin.y + this.attrs.height + modY
	);
	cx.lineTo(
		-center.x - this.origin.x + modX,
		-center.y - this.origin.y + modY
	);
	cx.closePath();
	cx.restore();
};

CanvasRect.prototype.createPointInPath = function(cx) {
	this.createFillPath(cx);
};

//returns center point as relative coordinates from base top left of object
CanvasRect.prototype.center = function() {
	return {
		x: this.attrs.width / 2,
		y: this.attrs.height / 2
	};
},
	
CanvasRect.prototype.box = function() {
	var center, width, height, sinRot, sinRot2;
	if (this.attrs.rotation === 0) {
		center = this.center();
		return {
			top: this.attrs.y - center.y - this.origin.y,
			right: this.attrs.x - center.x - this.origin.x + this.attrs.width,
			bottom: this.attrs.y - center.y - this.origin.y + this.attrs.height,
			left: this.attrs.x - center.x - this.origin.x,
			width: this.attrs.width,
			height: this.attrs.height
		};
	} else {
		center = this.rotatedCenter();
		sinRot = Math.abs(Math.sin(this.attrs.rotation));
		sinRot2 = Math.abs(Math.cos(this.attrs.rotation));
		width = this.attrs.height * sinRot + this.attrs.width * sinRot2;
		height = this.attrs.width * sinRot + this.attrs.height * sinRot2;
		
		return {
			top: Math.round(center.y - height / 2),
			right: Math.round(center.x + width / 2),
			bottom: Math.round(center.y + height / 2),
			left: Math.round(center.x - width / 2),
			width: width,
			height: height
		};
	}
};